﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames.FarseerPhysics.Collisions;
using Microsoft.Xna.Framework;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    public class TextureCollider : Collider
    {
        /// <summary>
        /// Creates a new instance of a TextureCollider.
        /// </summary>
        /// <param name="gameObject">The GameObject this Component should be attached to.</param>
        public TextureCollider(GameObject gameObject)
            : base(gameObject)
        {
            // If no RigidBody component is attached then attach a basic one without adding it to the physics simulator just so we can use Collider features.
            if (GameObject.RigidBody == null)
                GameObject.AddComponent<RigidBody>();

            Vertices verts = Vertices.CreatePolygon(GameObject.Renderer.TextureData, (Int32)GameObject.Transform.ScaledSize.X, (Int32)GameObject.Transform.ScaledSize.Y);
            
            mGeom = GeomFactory.Instance.CreatePolygonGeom(PhysicsManager.Instance.Simulator, GameObject.RigidBody.Body, verts, 0);
            
            mGeom.OnCollision += Collision;
            mGeom.OnSeparation += Seperation;

            mGeomColliderPairs.Add(mGeom, this);

            IsTrigger = false;
        }
    }
}
